home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / mat3.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  5.2 KB  |  148 lines

  1. /* Copyright 1988, Brown Computer Graphics Group.  All Rights Reserved. */
  2.  
  3. /* -------------------------------------------------------------------------
  4.                Public MAT3 include file
  5.    ------------------------------------------------------------------------- */
  6.  
  7. #ifndef MAT3_HAS_BEEN_INCLUDED
  8. #define MAT3_HAS_BEEN_INCLUDED
  9.  
  10. /* -----------------------------  Constants  ------------------------------ */
  11.  
  12. /*
  13.  * Make sure the math library .h file is included, in case it wasn't.
  14.  */
  15.  
  16. #ifndef HUGE
  17. #include <math.h>
  18. #endif
  19. #include <stdio.h>
  20.  
  21.  
  22. #define MAT3_DET0    -1            /* Indicates singular mat */
  23. #define MAT3_EPSILON    1e-12            /* Close enough to zero   */
  24. #define MAT3_PI     3.141592653589793    /* Pi              */
  25.  
  26. /* ------------------------------  Types  --------------------------------- */
  27.  
  28. typedef double MAT3mat[4][4];        /* 4x4 matrix             */
  29. typedef double MAT3vec[3];        /* Vector             */
  30. typedef double MAT3hvec[4];             /* Vector with homogeneous coord */
  31.  
  32. /* ------------------------------  Macros  -------------------------------- */
  33.  
  34. /* Tests if a number is within EPSILON of zero */
  35. #define MAT3_IS_ZERO(N)     ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
  36.  
  37. /* Sets a vector to the three given values */
  38. #define MAT3_SET_VEC(V,X,Y,Z)    ((V)[0]=(X), (V)[1]=(Y), (V)[2]=(Z))
  39.  
  40. /* Tests a vector for all components close to zero */
  41. #define MAT3_IS_ZERO_VEC(V)    (MAT3_IS_ZERO((V)[0]) && \
  42.                  MAT3_IS_ZERO((V)[1]) && \
  43.                  MAT3_IS_ZERO((V)[2]))
  44.  
  45. /* Dot product of two vectors */
  46. #define MAT3_DOT_PRODUCT(V1,V2) \
  47.             ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2])
  48.  
  49. /* Copy one vector to other */
  50. #define MAT3_COPY_VEC(TO,FROM)    ((TO)[0] = (FROM)[0], \
  51.                  (TO)[1] = (FROM)[1], \
  52.                  (TO)[2] = (FROM)[2])
  53.  
  54. /* Normalize vector to unit length, using TEMP as temporary variable.
  55.  * TEMP will be zero if vector has zero length */
  56. #define MAT3_NORMALIZE_VEC(V,TEMP) \
  57.     if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \
  58.        TEMP = 1.0 / TEMP; \
  59.        MAT3_SCALE_VEC(V,V,TEMP); \
  60.     } else TEMP = 0.0
  61.  
  62. /* Scale vector by given factor, storing result vector in RESULT_V */
  63. #define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \
  64.     MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE))
  65.  
  66. /* Adds vectors V1 and V2, storing result in RESULT_V */
  67. #define MAT3_ADD_VEC(RESULT_V,V1,V2) \
  68.     MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
  69.                    (V1)[2]+(V2)[2])
  70.  
  71. /* Subtracts vector V2 from V1, storing result in RESULT_V */
  72. #define MAT3_SUB_VEC(RESULT_V,V1,V2) \
  73.     MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
  74.                    (V1)[2]-(V2)[2])
  75.  
  76. /* Multiplies vectors V1 and V2, storing result in RESULT_V */
  77. #define MAT3_MULT_VEC(RESULT_V,V1,V2) \
  78.     MAT3_SET_VEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
  79.                    (V1)[2]*(V2)[2])
  80.  
  81. /* Sets RESULT_V to the linear combination of V1 and V2, scaled by
  82.  * SCALE1 and SCALE2, respectively */
  83. #define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \
  84.     MAT3_SET_VEC(RESULT_V,    (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \
  85.                 (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \
  86.                 (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2])
  87.  
  88. /* Several of the vector macros are useful for homogeneous-coord vectors */
  89. #define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \
  90.                   (V)[2]=(Z), (V)[3]=(W))
  91.  
  92. #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
  93.                  (TO)[1] = (FROM)[1], \
  94.                  (TO)[2] = (FROM)[2], \
  95.                  (TO)[3] = (FROM)[3])
  96.  
  97. #define MAT3_SCALE_HVEC(RESULT_V,V,SCALE) \
  98.     MAT3_SET_HVEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), \
  99.                 (V)[2]*(SCALE), (V)[3]*(SCALE))
  100.  
  101. #define MAT3_ADD_HVEC(RESULT_V,V1,V2) \
  102.     MAT3_SET_HVEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
  103.                 (V1)[2]+(V2)[2], (V1)[3]+(V2)[3])
  104.  
  105. #define MAT3_SUB_HVEC(RESULT_V,V1,V2) \
  106.     MAT3_SET_HVEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
  107.                 (V1)[2]-(V2)[2], (V1)[3]-(V2)[3])
  108.  
  109. #define MAT3_MULT_HVEC(RESULT_V,V1,V2) \
  110.     MAT3_SET_HVEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
  111.                 (V1)[2]*(V2)[2], (V1)[3]*(V2)[3])
  112.  
  113. /* ------------------------------  Entries  ------------------------------- */
  114.  
  115.  
  116. /* In MAT3geom.c */
  117. void         MAT3direction_matrix (MAT3mat result_mat, MAT3mat mat);
  118. int         MAT3normal_matrix (MAT3mat result_mat, MAT3mat mat);
  119. void        MAT3rotate (MAT3mat result_mat, MAT3vec axis, double angle_in_radians);
  120. void        MAT3translate (MAT3mat result_mat, MAT3vec trans);
  121. void        MAT3scale (MAT3mat result_mat, MAT3vec scale);
  122. void        MAT3shear(MAT3mat result_mat, double xshear, double yshear);
  123.  
  124. /* In MAT3mat.c */
  125. void        MAT3identity(MAT3mat);
  126. void        MAT3zero(MAT3mat);
  127. void        MAT3copy (MAT3mat to, MAT3mat from);
  128. void        MAT3mult (MAT3mat result, MAT3mat, MAT3mat);
  129. void        MAT3transpose (MAT3mat result, MAT3mat);
  130. int            MAT3invert (MAT3mat result, MAT3mat);
  131. void        MAT3print (MAT3mat, FILE *fp);
  132. void        MAT3print_formatted (MAT3mat, FILE *fp, 
  133.                       char *title, char *head, char *format, char *tail);
  134. extern int        MAT3equal();
  135. extern double        MAT3trace();
  136. extern int        MAT3power();
  137. extern int        MAT3column_reduce();
  138. extern int        MAT3kernel_basis();
  139.  
  140. /* In MAT3vec.c */
  141. void        MAT3mult_vec(MAT3vec result_vec, MAT3vec vec, MAT3mat mat);
  142. int        MAT3mult_hvec (MAT3hvec result_vec, MAT3hvec vec, MAT3mat mat, int normalize);
  143. void        MAT3cross_product(MAT3vec result,MAT3vec,MAT3vec);
  144. void        MAT3perp_vec(MAT3vec result_vec, MAT3vec vec, int is_unit);
  145.  
  146. #endif MAT3_HAS_BEEN_INCLUDED
  147.  
  148.